<?php
require_once 'Datatype_string.php';

class Datatype_localizedstring extends Datatype_string
{

    private $_langid = false;


    private $_localizedValue;

   	public function __construct(EDO_object_basic $owner, $config = array())
	{
		parent::__construct($owner, $config);
		
		$handler = $this->getOwner()->getHandler();
        $callback = $handler->getAttribute('Callback_DefineLangId');
	    $this->_langid = call_user_func($callback);

	    $this->getOwner()->attach($this, 'onSaveSuccess', '_save');
		$this->getOwner()->attach($this, 'onInsertSuccess', '_save');
		$this->getOwner()->attach($this, 'afterFetch', '_getLocalizedValue');
		$this->getOwner()->attach($this, 'onDelete', '_delete');

		
	}
	
	
	public function _delete()
	{
        $phrase = "DELETE FROM datatype_localizedstring WHERE edoid = '{$this->getOwner()->edoid}' AND fieldname = '{$this->getName()}' AND langid = '{$this->_langid}'";
        $this->getOwner()->ldb->exec($phrase);
	}


	protected function _getLocalizedValue()
	{
	    $phrase = "SELECT localizedstring FROM datatype_localizedstring WHERE edoid = '{$this->getOwner()->edoid}' AND fieldname = '{$this->getName()}' AND langid = '{$this->_langid}'";
	    $jaari = $this->getOwner()->ldb->getOne($phrase);
	    $this->_localizedValue = $this->getOwner()->ldb->getOne($phrase);
	}



	protected function _createTable()
	{
	    $phrase = "CREATE TABLE datatype_localizedstring(edoid integer not null REFERENCES edo_objects ON UPDATE cascade ON DELETE cascade, fieldname varchar(255) not null, langid char(2) not null, localizedstring varchar(255), PRIMARY KEY(edoid, fieldname, langid))";
	    $this->getOwner()->ldb->exec($phrase);

	}


	public function getValue()
	{
	    return $this->_localizedValue;
	}


	public function setValue($value)
	{
	    $value = $this->validate($value);
		if($value !== false)
		{
			$this->_localizedValue = $value;
		}

		return ($value === false) ? false : true;
	}


	protected function _save()
	{
	    $phrase = "DELETE FROM datatype_localizedstring WHERE edoid = '{$this->getOwner()->edoid}' AND fieldname = '{$this->getName()}' AND langid = '{$this->_langid}'";
	    $this->getOwner()->ldb->exec($phrase);

	    $phrase = "INSERT INTO datatype_localizedstring VALUES ('{$this->getOwner()->edoid}', '{$this->getName()}', '{$this->_langid}', '" . $this->_localizedValue . "')";
	    return $this->getOwner()->ldb->exec($phrase);

	}


	public function fetchMassData($fetcherObj, $config = array())
	{
	   	$relatedObj = $this->getOwner();

	    LEAD::requirePackage('SQL/Query');
		$query = new Query($fetcherObj->ldb);
		$table = $relatedObj->getFieldsTable($this->getName());


		$query->addField("{$table}.edoid AS rkey", true);

		$query->addField("datatype_localizedstring.localizedstring as rvalue", true)		;

		$query->getCurrentWhereGroup()->addWhereCond("{$table}.edoid", "=", "datatype_localizedstring.edoid");

		$query->getCurrentWhereGroup()->addWhereCond('datatype_localizedstring.fieldname', "=", "'{$this->getName()}'");
		$query->getCurrentWhereGroup()->addWhereCond('datatype_localizedstring.langid', "=", "'{$this->_langid}'");

		if(isset($config['order']))
		{
			foreach($config['order'] as &$orderer)
				$query->addOrderer($orderer);
		}

		if(isset($config['restrictor']) AND $fetcherObj->isFetched())
		{
			if($config['restrictor'])
			{
				/* @var $field Datatype_relation_1tomany */
				$field =& $config['restrictor']['fieldobject'];

				$fName = $field->getName();

				$obu = $field->getParent();

				$table = $fetcherObj->ldb->getTable($obu->getFieldsTable($fName));

				$query->addJoin($table, 'INNER', "{$table->getName()}.{$fName} = {$config['restrictor']['fieldvalue']}", 'joinfaker');
			}
		}

		$phrase = $query->getSQL();



		$res = $fetcherObj->ldb->getAll($phrase);
		$rez = array();
		foreach($res as &$row)
		{
			$rez[$row['rkey']] = $row;
		}

		return $rez;





	}











}
